package cn.com.callback


import cn.com.elite.EliteBean
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.json.JSONException

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 *
 * @Title: apiHST_KM_ranking
 * @Description: 华盛通 每天凌晨更新知识库排行榜
 * @author: 匡涛
 * @date 2021-7-5 12:50
 *
 */

public class apiHST_KM_ranking implements ServiceBackInvoke{
	private static final Log log = LogFactory.getLog(apiHST_KM_ranking.class);
	public String doCallBack(EliteBean eliteBean,HttpServletRequest request,HttpServletResponse response) {

		try {
			log.debug("=================state 开始  apiHST_KM_ranking===========" );

			String[] data1=new String[6];
			data1[0] = "delete from KM_TOPARTICLE where TOPARTICLETYPE=1  ";
			data1[1] = "insert into km_toparticle(kmguid,article_a_id,CLICKS_RATIO,TOPARTICLETYPE,ORDERINDEX)  " +
					"select  uuid() kmguid,article_a_id,click_ratio,'1',null from KM_CLICKRATIONSTATISTICS  " +
					"where statisticday=CONCAT(DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 DAY),'%Y-%m-%d'),' 00:00:00') " +
					"order by click_ratio desc limit 10";
			data1[2] = "delete from KM_TOPARTICLE where TOPARTICLETYPE=2  ";
			data1[3] = "insert into km_toparticle(kmguid,article_a_id,CLICKS_RATIO,TOPARTICLETYPE,ORDERINDEX)  " +
					"select kmguid,article_a_id,total,'2',null from  " +
					"( select article_a_id,sum(click_ratio) total, uuid() kmguid from km_clickrationstatistics  " +
					"where YEARWEEK(date_format(statisticday,'%Y-%m-%d'),1) = YEARWEEK(now(),1)-1 GROUP BY article_a_id  " +
					"order by total desc ) as a limit 10 " ;
			data1[4] = "delete from KM_TOPARTICLE where TOPARTICLETYPE=3  ";
			data1[5] = "insert into km_toparticle(kmguid,article_a_id,CLICKS_RATIO,TOPARTICLETYPE,ORDERINDEX)  " +
					"select kmguid,article_a_id,total,'3',null from  " +
					"(select uuid() kmguid ,article_a_id,sum(click_ratio) total from KM_CLICKRATIONSTATISTICS   " +
					"where PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( statisticday, '%Y%m' ) ) =1 GROUP BY article_a_id " +
					"order by total desc) as a limit 10 " ;
						log.debug("获取是否成功:" + data1.toString());
			int[] dataresult1 = eliteBean.updateBatch(data1, "dataSource");
			log.debug("知识库排行榜:" + dataresult1.toString());
			log.debug("=================end 结束  apiHST_KM_ranking===========" );
			return "{\"code\":\"1\",\"message\":\"成功\",\"data\":{\"endabled\":\"凌晨更新知识库排行榜\"}}";
		} catch (JSONException e) {
			log.error( "处理异常，失败：" + e.getMessage());
		}
	}

}